const ltlTree = {
  state: {
    checkRules: {
    },

    ltlTree: []
  },
  mutations: {
    setLtlTree (state, data) {
      state.ltlTree = data
    }
  },
  actions: {
    async getLtlTree (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/ltl/tree/'
      })
      context.commit('setLtlTree', [response.data])
      if (response.data) {
        context.commit('setSelfNetworkUid', response.data.uid)
      }
    }
  },
  getters: {
    LtlTreeChart (state) {
      return processLtlTree(state.ltlTree)
    }
  }
}

function processLtlTree (data) {
  return [...data.map(item => {
    let recursiveData = []
    for (let key in item.children) {
      recursiveData.push(item.children[key])
    }
    return {
      uid: item.uid,
      name: item.name,
      address: item.address,
      is_online: item.is_online,
      itemStyle: {
        color: item.is_online ? '#5d88c1' : '#d2d2d2',
        borderWidth: 1,
        borderColor: '#000'
      },
      children: processLtlTree(recursiveData)
    }
  })]
}

export default ltlTree
